import math
import random

import pygame

from actor import WalkDir, ShootDir
from actor.shoot_bullet import Bullet
from actor.shoot_soldier import ShootSoldier

"""
boss类
"""


class Boss(ShootSoldier):
    def __init__(self, x, y, xiao_tie):
        """
        参数初始化
        :param x: x坐标
        :param y: y坐标
        :param xiao_tie: 小铁精灵类
        """
        super(Boss, self).__init__("./resources/images/japanese/soldier4.png",
                                   "./resources/images/japanese/soldier4-1.png",
                                   x, y, "最终boos", 1000)

        self.xiao_tie = xiao_tie
        # 时间戳
        self.time_stamp = 10
        # 移动速度
        self.move_speed = 1
        # 移动方向枚举
        self.dir = WalkDir.up
        # 子弹精灵类
        self.bullets = pygame.sprite.Group()
        # 射击方向枚举
        self.shoot_action.shoot_dir = ShootDir.up
        self.walk_action.set_dir(WalkDir.up)

    def draw(self, surface, x: int, y: int):
        """
        在窗口上对应位置绘制出相应的图片
        :param surface:
        :param x:
        :param y:
        :return:
        """
        if self.shoot_action.shoot_dir == ShootDir.not_shoot:
            super(ShootSoldier, self).draw(surface, x, y)
        elif self.shoot_action.shoot_dir == ShootDir.down:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
            image = self.walk_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        elif self.shoot_action.shoot_dir == ShootDir.up:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - 60 - y))
            image = self.walk_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        elif self.shoot_action.shoot_dir == ShootDir.left_up:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
            image = self.walk_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        elif self.shoot_action.shoot_dir == ShootDir.left_down:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
            image = self.walk_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        elif self.shoot_action.shoot_dir == ShootDir.right_down:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
            image = self.walk_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        elif self.shoot_action.shoot_dir == ShootDir.right_up:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
            image = self.walk_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        for bullet in self.bullets:
            bullet.draw(surface, x - 35, y + 20)

    def check_dis(self, obstacle_group):
        """
        检测主角和自己的距离
        :param obstacle_group: 障碍物精灵组
        :return:
        """
        off_x = 0
        off_y = 0
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        xiao_x = self.xiao_tie.pos_x
        xiao_y = self.xiao_tie.pos_y
        dis = math.pow((xiao_x - self.pos_x), 2) + math.pow((xiao_y - self.pos_y), 2)
        sqrt_dis = math.sqrt(dis)
        if sqrt_dis < 100:
            if self.time_stamp == 0:
                self.__create_bullet(self.pos_x, self.pos_y)
                self.move_speed = 1
                self.time_stamp = 10
            else:
                self.time_stamp -= 1
        elif sqrt_dis < 150:
            if self.time_stamp == 0:
                self.__create_bullet(self.pos_x, self.pos_y)
                self.move_speed = 2
                self.time_stamp = 10
            else:
                self.time_stamp -= 1
        elif sqrt_dis < 200:
            if self.time_stamp == 0:
                self.__create_bullet(self.pos_x, self.pos_y)
                self.move_speed = 2
                self.time_stamp = 10
            else:
                self.time_stamp -= 1
        elif sqrt_dis < 300:
            self.move_speed = 3
        else:
            self.move_speed = 0
        if xiao_x > self.pos_x:
            self.pos_x += self.move_speed
            #  障碍物碰撞检测
            collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
            if len(collidate_list1) > 0:
                self.rect = temp_rect
            else:
                self.pos_x += off_x
            self.dir = WalkDir.right
            self.shoot_action.shoot_dir = ShootDir.right_up
            self.walk_action.set_dir(WalkDir.right)
        elif xiao_x < self.pos_x:
            self.pos_x -= self.move_speed
            #  障碍物碰撞检测
            collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
            if len(collidate_list1) > 0:
                self.rect = temp_rect
            else:
                self.pos_x += off_x
            self.dir = WalkDir.left
            self.shoot_action.shoot_dir = ShootDir.left_up
            self.walk_action.set_dir(WalkDir.left)
        else:
            if xiao_y > self.pos_y:
                self.pos_y += self.move_speed
                #  障碍物碰撞检测
                collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
                if len(collidate_list1) > 0:
                    self.rect = temp_rect
                else:
                    self.pos_y += off_y
                    self.dir = WalkDir.down
                    self.shoot_action.shoot_dir = ShootDir.down
                    self.walk_action.set_dir(WalkDir.down)
            else:
                self.pos_y -= self.move_speed
                #  障碍物碰撞检测
                collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
                if len(collidate_list1) > 0:
                    self.rect = temp_rect
                else:
                    self.pos_y += off_y
                    self.dir = WalkDir.up
                    self.shoot_action.shoot_dir = ShootDir.up
                    self.walk_action.set_dir(WalkDir.up)
        if xiao_y > self.pos_y:
            self.pos_y += self.move_speed
            #  障碍物碰撞检测
            collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
            if len(collidate_list1) > 0:
                self.rect = temp_rect
            else:
                self.pos_y += off_y
                self.dir = WalkDir.down
                self.shoot_action.shoot_dir = ShootDir.down
                self.walk_action.set_dir(WalkDir.down)
        elif xiao_y < self.pos_y:
            self.pos_y -= self.move_speed
            #  障碍物碰撞检测
            collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
            if len(collidate_list1) > 0:
                self.rect = temp_rect
            else:
                self.pos_x += off_x
                self.pos_y += off_y
                self.dir = WalkDir.up
                self.shoot_action.shoot_dir = ShootDir.up
                self.walk_action.set_dir(WalkDir.up)
        else:
            if xiao_x > self.pos_x:
                self.pos_x += self.move_speed
                #  障碍物碰撞检测
                collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
                if len(collidate_list1) > 0:
                    self.rect = temp_rect
                else:
                    self.pos_x += off_x
                    self.pos_y += off_y
                    self.dir = WalkDir.right
                    self.shoot_action.shoot_dir = ShootDir.right_up
                    self.walk_action.set_dir(WalkDir.right)
            elif xiao_x < self.pos_x:
                self.pos_x -= self.move_speed
                #  障碍物碰撞检测
                collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
                if len(collidate_list1) > 0:
                    self.rect = temp_rect
                else:
                    self.pos_x += off_x
                    self.dir = WalkDir.left
                    self.shoot_action.shoot_dir = ShootDir.left_up
                    self.walk_action.set_dir(WalkDir.left)

    def run(self, down_flag, key_list, obstacle_group):
        """
        实现具体移动细节
        :param down_flag:
        :param key_list:
        :param obstacle_group: 障碍物精灵组
        :return:
        """
        self.check_dis(obstacle_group)
        self.update_rect(self.pos_x, self.pos_y)
        if self.time_stamp == 0:
            self.__create_bullet(self.pos_x, self.pos_y)
            self.time_stamp = 10
        else:
            self.time_stamp -= 1
        for bullet in self.bullets:
            bullet.run()
        pygame.sprite.groupcollide(self.bullets, obstacle_group, True, False)
        # 如果子弹与小铁相碰，小铁损失相应生命值
        if pygame.sprite.spritecollide(self.xiao_tie, self.bullets, True):
            self.xiao_tie.hp -= random.randint(30, 50)

    def __create_bullet(self, x, y):
        """
        实现埋地雷操作
        :param x:
        :param y:
        :return:
        """
        bullet = Bullet(x, y, pygame.image.load("./resources/images/shoot/子弹.png"))
        bullet.set_dir(self.dir)
        self.bullets.add(bullet)

    def update_rect(self, x, y):
        """
        更新boss矩形框位置
        :param x: x坐标
        :param y: y坐标
        :return:
        """
        self.rect = pygame.Rect(x, y, self.image_width, self.image_height)
